<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
          "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>QUnit for JWS.verifyJWT method</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="qunit.css" type="text/css" media="screen" />

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="qunit.js"></script>

<script language="JavaScript" type="text/javascript" src="../jsrsasign-all-min.js"></script>
<script language="JavaScript" type="text/javascript" src="../ext/json-sans-eval.js"></script>
<script language="JavaScript" type="text/javascript" src="../src/base64x-1.1.js"></script>
<script language="JavaScript" type="text/javascript" src="../src/jws-3.3.js"></script>

<script type="text/javascript">
// tool_jwt.html with one aud
var jwtHS256AUD1 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2p3dC1pZHAuZXhhbXBsZS5jb20iLCJzdWIiOiJtYWlsdG86bWlrZUBleGFtcGxlLmNvbSIsIm5iZiI6MSwiZXhwIjoyMDgyNzU4Mzk5LCJpYXQiOjE0MzI5MTQ0MzMsImp0aSI6ImlkMTIzNDU2IiwidHlwIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9yZWdpc3RlciIsImF1ZCI6Imh0dHA6Ly9mb28xLmNvbSJ9.r2mRSoDobgrPg9zDlTEsyQNpua6aGId4UKRYnEo9KRk";

// tool_jwt.html with two aud
var jwtHS256AUD2 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2p3dC1pZHAuZXhhbXBsZS5jb20iLCJzdWIiOiJtYWlsdG86bWlrZUBleGFtcGxlLmNvbSIsIm5iZiI6MSwiZXhwIjoyMDgyNzU4Mzk5LCJpYXQiOjE0MzI5MTQyODMsImp0aSI6ImlkMTIzNDU2IiwidHlwIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9yZWdpc3RlciIsImF1ZCI6WyJodHRwOi8vZm9vMS5jb20iLCJodHRwOi8vZm9vMi5jb20iXX0.brHoaBSFRnGH3VspSgTW1H2VFNS1Ju_pWU4aomrjjq8";

// tool_jwt.html with iat=2000/01/01(946684800) nbf=2000/01/01(946684800) exp=2010/01/01(1262304000)
var jwtHS256TIME = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2p3dC1pZHAuZXhhbXBsZS5jb20iLCJzdWIiOiJtYWlsdG86bWlrZUBleGFtcGxlLmNvbSIsIm5iZiI6OTQ2Njg0ODAwLCJleHAiOjEyNjIzMDQwMDAsImlhdCI6OTQ2Njg0ODAwLCJqdGkiOiJpZDEyMzQ1NiIsInR5cCI6Imh0dHBzOi8vZXhhbXBsZS5jb20vcmVnaXN0ZXIiLCJhdWQiOiJodHRwOi8vZm9vMS5jb20ifQ.mKoQqwytXUtT3Y0Obp-j973pTyOxSZBS7NAaZ3BAyqg";

// tool_jwt.html no jti
var jwtHS256NOJTI = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2p3dC1pZHAuZXhhbXBsZS5jb20iLCJzdWIiOiJtYWlsdG86bWlrZUBleGFtcGxlLmNvbSIsIm5iZiI6MSwiZXhwIjoyMDgyNzU4Mzk5LCJpYXQiOjEsInR5cCI6Imh0dHBzOi8vZXhhbXBsZS5jb20vcmVnaXN0ZXIiLCJhdWQiOiJodHRwOi8vZm9vMS5jb20ifQ.fmG19deqNvXXbtEgQRBeaJqRYBac9HfAesqTF8z3nXc";

// jwt.io HS256 {iat: 946684800, nbf: 946684800, exp: 946684920} 
//   2min 2000/01/01 00:01:00 946684860 (verify at)
var jwtGrace = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gQmVlIiwiYWRtaW4iOnRydWUsImlhdCI6OTQ2Njg0ODAwLCJuYmYiOjk0NjY4NDgwMCwiZXhwIjo5NDY2ODQ5MjB9.XapAg9_7SaJovdN7L2HxJ3UrzWGhSEcgjVJLSNr658Q";

$(document).ready(function(){

test("inArray", function() {
  var ns1 = KJUR.jws.JWS;
  equal(ns1.inArray("a", null), false, "a in null");
  equal(ns1.inArray("a", {}), false, "a in {}");
  equal(ns1.inArray("a", ["b"]), false, "a in [b]");
  equal(ns1.inArray("a", ["b", "c"]), false, "a in [b,c]");
  equal(ns1.inArray("a", ["b", "a"]), true, "a in [b,a]");
  equal(ns1.inArray("a", ["a", "b"]), true, "a in [a,b]");
});

test("includedArray", function() {
  var ns1 = KJUR.jws.JWS;
  equal(ns1.includedArray(["a"], null), false, "[a] in null");
  equal(ns1.includedArray(["a"], {}), false, "[a] in {}");
  equal(ns1.includedArray(["a"], ["a"]), true, "[a] in [a]");
  equal(ns1.includedArray(["a"], ["b"]), false, "[a] in [b]");
  equal(ns1.includedArray(["a"], ["b", "c"]), false, "[a] in [b,c]");
  equal(ns1.includedArray(["a"], ["a", "b"]), true, "[a] in [a,b]");
  equal(ns1.includedArray(["a"], ["b", "a"]), true, "[a] in [b,a]");
  equal(ns1.includedArray(["a", "b"], ["b", "a", "c"]), true, "[a,b] in [b,a,c]");
});


test("verifyJWT alg ", function() {
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"]}),
        true, "HS256 in [HS256]");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["RS256"]}),
        false, "HS256 not in [RS256]");
});

test("verifyJWT iss ", function() {
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"], iss: ["https://jwt-idp.example.com"]}),
        true, "iss in acceptField.iss");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"], iss: ["ftp://no-host"]}),
        false, "iss not in acceptField.iss");
});

test("verifyJWT sub ", function() {
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"], sub: ["mailto:mike@example.com"]}),
        true, "iss in acceptField.sub");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"], sub: ["mailto:nobody@nobody.com"]}),
        false, "iss not in acceptField.sub");
});

test("verifyJWT aud ", function() {
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD1, "616161", {alg: ["HS256"], aud: ["http://foo1.com"]}),
        true, "single aud in acceptField.aud");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD1, "616161", {alg: ["HS256"], aud: ["http://not-match.com"]}),
        false, "single aud not in acceptField.aud");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"], aud: ["http://foo1.com", "http://foo2.com"]}),
        true, "two aud in acceptField.aud");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"], aud: ["http://not-match.com", "http://not-match2.com"]}),
        false, "two aud not in acceptField.aud");
});

test("verifyJWT exp ", function() {
  var JWS = KJUR.jws.JWS;
  var IntDate = KJUR.jws.IntDate;
  equal(JWS.verifyJWT(jwtHS256TIME, "616161", {alg: ["HS256"], verifyAt: IntDate.getZulu('20050101000000Z')}),
        true, "2005 < exp2010");
  equal(JWS.verifyJWT(jwtHS256TIME, "616161", {alg: ["HS256"], verifyAt: IntDate.getZulu('20150101000000Z')}),
        false, "exp2010 < 2015");
});

test("verifyJWT nbf ", function() {
  var JWS = KJUR.jws.JWS;
  var IntDate = KJUR.jws.IntDate;
  equal(JWS.verifyJWT(jwtHS256TIME, "616161", {alg: ["HS256"], verifyAt: IntDate.getZulu('20050101000000Z')}),
        true, "nbf2000 < 2005");
  equal(JWS.verifyJWT(jwtHS256TIME, "616161", {alg: ["HS256"], verifyAt: IntDate.getZulu('19950101000000Z')}),
        false, "1995 < nbf2000");
});

test("verifyJWT iat ", function() {
  var JWS = KJUR.jws.JWS;
  var IntDate = KJUR.jws.IntDate;
  equal(JWS.verifyJWT(jwtHS256TIME, "616161", {alg: ["HS256"], verifyAt: IntDate.getZulu('20050101000000Z')}),
        true, "iat2000 < 2005");
  equal(JWS.verifyJWT(jwtHS256TIME, "616161", {alg: ["HS256"], verifyAt: IntDate.getZulu('19950101000000Z')}),
        false, "1995 < iat2000");
});

test("verifyJWT jti", function() {
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"]}),
        true, "with jti");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256NOJTI, "616161", {alg: ["HS256"]}),
        true, "without jti");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"], jti: "id3"}),
        false, "id123456 != id3");
});

test("verifyJWT JWS signature", function() {
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616161", {alg: ["HS256"]}),
        true, "HS256 valid key");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2, "616162", {alg: ["HS256"]}),
        false, "HS256 wrong key");
  equal(KJUR.jws.JWS.verifyJWT(jwtHS256AUD2.replace("L2p3d", "L2o3d"), "616161", {alg: ["HS256"]}),
        false, "iss jwt-idp to j7t-idp forged signature");
});

test("verifyJWT gracePeriod test", function() {
  var JWS = KJUR.jws.JWS;
  var IntDate = KJUR.jws.IntDate;
  equal(JWS.verifyJWT(jwtGrace, "616161", {alg: ["HS256"], verifyAt: 946684860}),
        true, "iat2000 < 2000+1min ? true");
  equal(JWS.verifyJWT(jwtGrace, "616161", {alg: ["HS256"], verifyAt: 946684860 - 120}),
        false, "iat2000 < 2000-1min ? false");
  equal(JWS.verifyJWT(jwtGrace, "616161", {alg: ["HS256"], verifyAt: 946684860 + 120}),
        false, "2000+3min < exp2000+2min ? false");
  equal(JWS.verifyJWT(jwtGrace, "616161", {alg: ["HS256"], verifyAt: 946684860 - 120, gracePeriod: 2 * 60}),
        true, "iat2000 < 2000-1min ? grace=2min true");
  equal(JWS.verifyJWT(jwtGrace, "616161", {alg: ["HS256"], verifyAt: 946684860 + 120, gracePeriod: 2 * 60}),
        true, "2000+3min < exp2000+2min ? grace=2min true");
});

});
</script>
  
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">test markup</div>
<a href="index.html">INDEX</a> | 
</body>
</html>
